﻿using System;
using System.Web;
using System.Web.UI;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using NntpClientLib;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

namespace NewsgroupListSync
{
    /// <summary>
    /// Summary description for NewMessage
    /// </summary>
    public partial class NewMessage : LayoutsPageBase
    {
        #region "Private Identifiers"

        protected int articleID;
        private string newsServer = "news.mathworks.com";
        private string newsGroup = "";

        private IEnumerable<string> articleBody;

        #endregion

        #region "Event Handlers"

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["NewsServer"] != null)
                newsServer = Request.QueryString["NewsServer"].ToString();
            NewsServerTextBox.Text = newsServer;

            if (Request.QueryString["NewsGroup"] != null)
                newsGroup = Request.QueryString["NewsGroup"].ToString();
            NewsGroupTextBox.Text = newsGroup;
            newsgroupName.Text = newsGroup;

            if (!IsPostBack)
                FromTextBox.Text = GetLoginUserEmailAddress();
        }

        protected override void OnPreInit(EventArgs e)
        {
            base.OnPreInit(e);

            if (SPContext.Current != null)
            {
                MasterPageFile = SPContext.Current.Web.MasterUrl;
            }
        }

        protected void SubmitButton_Click(object sender, EventArgs e)
        {
            try
            {
                using (Rfc977NntpClient client = new Rfc977NntpClient())
                {
                    client.Connect(newsServer);
                    client.SelectNewsgroup(newsGroup);

                    ArticleHeadersDictionary headers = new ArticleHeadersDictionary();
                    headers.AddHeader("From", FromTextBox.Text);
                    headers.AddHeader("Subject", SubjectTextBox.Text);
                    headers.AddHeader("Newsgroups", newsGroup);
                    //headers.AddHeader("Date", DateTime.Now.ToString());

                    List<string> body = new List<string>();
                    body.Add(MessageTextBox.Text);

                    client.PostArticle(new ArticleHeadersDictionaryEnumerator(headers), body);

                    //StatusLabel.Text = "Message has been posted!";
                    Response.Redirect("/_layouts/newsgroups/Thankyou.aspx?Newsgroup=" + newsGroup);
                }
            }
            catch (Exception ex)
            {
                StatusLabel.Text = ex.Message;
            }
        }

        protected void Cancel_Click(object sender, EventArgs e)
        {
            Response.Redirect(SPContext.Current.Web.Url + "/Lists/" + Regex.Replace(newsGroup, "[^A-Za-z0-9]", ""));
        }

        #endregion

        #region "Private Routines"

        private string GetLoginUserEmailAddress()
        {
            if (Context.User.Identity.IsAuthenticated)
            {
                SPUser myUser = SPContext.Current.Web.CurrentUser;
                if (myUser != null)
                    return String.Format("{0} ({1})", myUser.Email, myUser.Name);
                else
                    return "";
            }
            else
                return "";
        }
        #endregion

    }
}